package org.ielse.frame.test;

import java.io.File;

import org.ielse.frame.App;
import org.ielse.frame.BaseActivity;
import org.ielse.frame.C;
import org.ielse.frame.R;
import org.ielse.frame.t.volley.ImageLoader;
import org.ielse.frame.t.volley.ImageLoader.ImageContainer;
import org.ielse.frame.util.BaseUtil;
import org.ielse.frame.util.LogUtil;
import org.ielse.frame.view.PictureView;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

public class PictureActivity extends BaseActivity {
	public static final String EXTRA_URL = "extra_url";
	public static final String EXTRA_X = "extra_x";
	public static final String EXTRA_Y = "extra_y";

	private View layContainer, layBackground;
	private PictureView imgPicture;

	private int originalX, originalY, centerX, centerY;

	@SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_picture);
		super.onCreate(savedInstanceState);
		useDefaultTransitionAnim = false;

		layBackground = findViewById(R.id.lay_background);
		layContainer = findViewById(R.id.lay_container);

		imgPicture = (PictureView) findViewById(R.id.img_picture);

		String url = getIntent().getStringExtra(EXTRA_URL);
		if (!TextUtils.isEmpty(url)) {
			ImageContainer container = App
					.i()
					.getImageLoader()
					.get(url,
							ImageLoader.getImageListener(imgPicture, R.drawable.test_img_default, R.drawable.test_img_loading,
									R.drawable.test_img_fail));
			centerX = (BaseUtil.getScreenWidth() - container.getBitmap().getWidth()) / 2;
			centerY = (BaseUtil.getScreenHeight() - container.getBitmap().getHeight() - BaseUtil.getStatusBarHeight()) / 2;
			LogUtil.d("centerX:" + centerX + ",centerY:" + centerY);

			originalX = getIntent().getIntExtra(EXTRA_X, 0);
			originalY = getIntent().getIntExtra(EXTRA_Y, 0);
			if (originalX != 0 || originalY != 0) {
				imgPicture.setTranslationX(originalX - centerX);
				imgPicture.setTranslationY(originalY - centerY);
			}
		}

		ObjectAnimator anim1 = ObjectAnimator.ofFloat(imgPicture, "translationX", originalX - centerX, 0);
		ObjectAnimator anim2 = ObjectAnimator.ofFloat(imgPicture, "translationY", originalY - centerY, 0);
		ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgPicture, "scaleX", 1, 2);
		ObjectAnimator anim4 = ObjectAnimator.ofFloat(imgPicture, "scaleY", 1, 2);
		ObjectAnimator anim5 = ObjectAnimator.ofFloat(imgPicture, "rotation", 0, 360);
		ObjectAnimator anim6 = ObjectAnimator.ofFloat(layContainer, "alpha", 0, 1);

		AnimatorSet set = new AnimatorSet();
		set.play(anim1).with(anim2).with(anim3).with(anim4).with(anim5).with(anim6);
		set.setInterpolator(new DecelerateInterpolator());
		set.setDuration(600);
		set.start();
		set.addListener(new AnimatorListenerAdapter() {
			@Override public void onAnimationEnd(Animator animation) {
				super.onAnimationEnd(animation);
			}
		});

		layContainer.setOnTouchListener(new View.OnTouchListener() {
			@Override public boolean onTouch(View view, MotionEvent event) {
				imgPicture.dealTouchEvent(event);
				return true;
			}
		});

		File file = new File(App.i().getCacheDir(), C.settings.PICTURE_TRANS_BACK);
		Bitmap background = BitmapFactory.decodeFile(file.getAbsolutePath());
		layBackground.setBackgroundDrawable(new BitmapDrawable(getResources(), background));
	}

	@Override public void onBackPressed() {
		finishAnim();
	}

	private void finishAnim() {
		ObjectAnimator anim1 = ObjectAnimator.ofFloat(imgPicture, "translationX", 0, originalX - centerX);
		ObjectAnimator anim2 = ObjectAnimator.ofFloat(imgPicture, "translationY", 0, originalY - centerY);
		ObjectAnimator anim3 = ObjectAnimator.ofFloat(imgPicture, "scaleX", imgPicture.getScaleX(), 1);
		ObjectAnimator anim4 = ObjectAnimator.ofFloat(imgPicture, "scaleY", imgPicture.getScaleY(), 1);
		ObjectAnimator anim5 = ObjectAnimator.ofFloat(imgPicture, "rotation", 360, 0);
		ObjectAnimator anim6 = ObjectAnimator.ofFloat(layContainer, "alpha", 1, 0);
		anim5.setInterpolator(new DecelerateInterpolator());

		AnimatorSet set = new AnimatorSet();
		set.play(anim1).with(anim2).with(anim3).with(anim4).with(anim5).with(anim6);
		set.setDuration(600);
		set.start();
		set.addListener(new AnimatorListenerAdapter() {
			@Override public void onAnimationEnd(Animator animation) {
				super.onAnimationEnd(animation);
				finish();
			}
		});
	}

}
